<template>
  <el-container class="layout-container-demo">
    <el-aside width="200px">
      <el-scrollbar>
        <el-menu :default-openeds="['1', '2', '3', '4', '5', '6']" @select="handleMenuSelect">
          <el-sub-menu index="1">
            <template #title>
              <el-icon><message /></el-icon>首页
            </template>
            <el-menu-item index="/adminLayout/DashboardsAdmin">Dashboard</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="2">
            <template #title>
              <el-icon><icon-menu /></el-icon>功能管理
            </template>
            <el-menu-item index="/adminLayout/FunctionAdmin1">Function1</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="3">
            <template #title>
              <el-icon><View /></el-icon>历史记录管理
            </template>
            <el-menu-item index="/adminLayout/HistoryAdmin1">History Admin 1</el-menu-item>
            <el-menu-item index="/adminLayout/HistoryAdmin2">History Admin 2</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="4">
            <template #title>
              <el-icon><UserFilled /></el-icon>用户管理
            </template>
            <el-menu-item index="/adminLayout/UserAdmin1">User Admin 1</el-menu-item>
            <el-menu-item index="/adminLayout/UserAdmin2">User Admin 2</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="5">
            <template #title>
              <el-icon><User /></el-icon>个人信息管理
            </template>
            <el-menu-item index="/adminLayout/PersonMessageAdmin1">Person Message Admin 1</el-menu-item>
            <el-menu-item index="/adminLayout/PersonMessageAdmin2">Person Message Admin 2</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="6">
            <template #title>
              <el-icon><Phone /></el-icon>客服通信管理
            </template>
            <el-menu-item index="/adminLayout/CommunicationAdmin">Communication Admin</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <el-container>
      <el-header class="custom-header">
        <div class="header-title">管理员界面</div>
        <div class="toolbar">
          <span>{{ adminName }}</span>
          <el-dropdown>
            <el-icon style="margin-left:8px;">
              <setting />
            </el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>联系</el-dropdown-item>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>


      <el-main>
        <el-scrollbar>
          <router-view />
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Menu as IconMenu, Message, Phone, Setting, User, UserFilled, View } from '@element-plus/icons-vue'
import { useRouter } from "vue-router";
const router = useRouter();

// 从 localStorage 读取管理员名字
function getAdminName() {
  const info = localStorage.getItem("userInfo");
  if (info) {
    try {
      const obj = JSON.parse(info);
      return obj.name || '';
    } catch (e) {
      return '';
    }
  }
  return '';
}
const adminName = ref(getAdminName());

// 处理菜单项选择
const handleMenuSelect = (index: string) => {
  router.push(index); // 使用 router.push 跳转到选中的路由
};

// 处理退出登录
const handleLogout = () => {
  localStorage.removeItem("token");
  localStorage.removeItem("userInfo");

  router.push('/login');
};
</script>

<style scoped>
.layout-container-demo {
  height: 100vh; /* 让父容器占满整个视口 */
}
.layout-container-demo .el-header {
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}
.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);
}
.layout-container-demo .el-menu {
  border-right: none;
}
.layout-container-demo .el-main {
  padding: 0;
}
.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}
.custom-header {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #cce3fc; /* 跟你之前的蓝色 */
  position: relative;
  height: 80px;
  padding: 0 30px;
}
.header-title {
  flex: 1;
  text-align: center;
  font-size: 40px;
  font-weight: bold;
  letter-spacing: 6px;
  color: #222;
}
.toolbar {
  display: flex;
  align-items: center;
  position: absolute;
  right: 36px;
  top: 0;
  height: 100%;
  font-size: 18px;
  color: #222;
}
.toolbar span {
  margin-right: 4px;
  font-size: 20px;
}

</style>
